package com.beiing.tianshuai.tianshuai.fragment;


import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.LinearLayout;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.adapter.EmotionGridViewAdapter;
import com.beiing.tianshuai.tianshuai.adapter.EmotionPagerAdapter;
import com.beiing.tianshuai.tianshuai.util.DisplayUtil;
import com.beiing.tianshuai.tianshuai.util.EmotionUtils;
import com.beiing.tianshuai.tianshuai.util.GlobalOnItemClickManagerUtils;
import com.beiing.tianshuai.tianshuai.util.LogUtils;
import com.beiing.tianshuai.tianshuai.widget.EmojiIndicatorView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by LZQ on 2018/1/3.
 * Description:可替换的模板表情，gridView实现
 */

public class EmotionCompleteFragment extends Fragment {

    @BindView(R.id.vp_complete_emotion_layout)
    ViewPager vpCompleteEmotionLayout;
    @BindView(R.id.ll_point_group)
    EmojiIndicatorView llPointGroup;

    Unbinder unbinder;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_complete_emotion, container, false);
        unbinder = ButterKnife.bind(this, rootView);
        initListener();
        initEmotion();
        return rootView;
    }

    private void initListener() {
        vpCompleteEmotionLayout.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            int oldPagerPos = 0;
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                llPointGroup.playByStartPointToNext(oldPagerPos, position);
                oldPagerPos = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     *  初始化表情面板
     */
    @TargetApi(Build.VERSION_CODES.KITKAT)
    private void initEmotion(){
        // 获取屏幕宽度
        int screenWidth = DisplayUtil.getMobileWidth(getActivity());
        // item的间距
        int spacing = DisplayUtil.dip2px(getActivity(), 12);
        // 动态计算item的宽度和高度
        int itemWidth = (screenWidth - spacing * 7) / 7;
        // 动态计算gridView的总宽度
        int gvHeight = itemWidth * 3 + spacing * 4;

        List<GridView> emotionViews = new ArrayList<>();
        List<String> emotionNames = new ArrayList<>();
        for (String emojiName : EmotionUtils.getEmojiMap().keySet()){
            emotionNames.add(emojiName);
            //每20个表情作为一组，同时添加到ViewPager对应的view集合中
            if (emotionNames.size() == 20){
                GridView gv = createEmotionGridView(emotionNames, screenWidth, spacing, itemWidth, gvHeight);
                emotionViews.add(gv);
                // 添加完一组，重新创建一个表情名字集合
                emotionNames = new ArrayList<>();
            }
        }

        // 判断最后是否有不足20个表情的剩余情况
        if (emotionNames.size() > 0){
            GridView gv = createEmotionGridView(emotionNames, screenWidth, spacing, itemWidth, gvHeight);
            emotionViews.add(gv);
        }

        // 初始化指示器
        llPointGroup.initIndicator(emotionViews.size());
        // 将多个GridView添加显示到ViewPager中
        EmotionPagerAdapter emotionPagerAdapter = new EmotionPagerAdapter(emotionViews);
        vpCompleteEmotionLayout.setAdapter(emotionPagerAdapter);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(screenWidth, gvHeight);
        vpCompleteEmotionLayout.setLayoutParams(params);
    }

    private GridView createEmotionGridView(List<String> emotionNames, int gvWidth, int padding, int itemWidth, int gvHeight){
        GridView gv = new GridView(getActivity());
        gv.setSelector(R.color.transparent);
        gv.setNumColumns(7);
        gv.setPadding(padding, padding, padding, padding);
        gv.setHorizontalSpacing(padding);
        gv.setVerticalSpacing(padding);

        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(gvWidth, gvHeight);
        gv.setLayoutParams(params);
        EmotionGridViewAdapter adapter = new EmotionGridViewAdapter(getActivity(), emotionNames, itemWidth);
        gv.setAdapter(adapter);
        gv.setOnItemClickListener(GlobalOnItemClickManagerUtils.getInstance(getActivity()).getOnItemClickListener());
        return gv;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
